package com.smdk.dsminio.controller;


import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smdk.dsminio.apiservice.FileService;
import com.smdk.dsminio.comment.BaseController;
import com.smdk.dsminio.entity.DsminioFileInfo;
import com.smdk.dsminio.redis.RedisUtil;
import com.smdk.dsminio.service.DsminioFileInfoService;
import com.smdk.dsminio.service.DsminioFolderInfoService;
import com.smdk.dsminio.utils.AjaxResult;
import com.smdk.dsminio.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.*;


/**
 * @author 神秘的凯
 * @since 2020-03-19
 */
@Api(tags = {"ObjectInfo"})
@RestController
@RequestMapping("/api/")
public class DsminioFileInfoController extends BaseController {
    @Autowired
    private DsminioFileInfoService dsminioFileInfoService;
    @Autowired
    private DsminioFolderInfoService dsminioFolderInfoService;
    /**
     * 创建File信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/insertDsminioFileInfo", method = RequestMethod.POST)
    @ApiOperation(value = "创建File信息")
    public @ResponseBody
    AjaxResult insertDsminioFileInfo(@RequestBody DsminioFileInfo dsminioFileInfo) {
        dsminioFileInfoService.addFileInfo(dsminioFileInfo);
        return AjaxResult.success(true);
    }


    /**
     * 删除文件
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteFileInfo", method = RequestMethod.POST)
    @ApiOperation(value = "删除文件")
    public @ResponseBody
    AjaxResult deleteObjectList(@ApiParam(value = "fileIds", required = true) @RequestParam(value = "fileIds") List<Long> objectIds) {
        for (Long objectId : objectIds) {
            DsminioFileInfo dsminioFileInfo = dsminioFileInfoService.getById(objectId);
            FileUtil.del(System.getProperty("user.dir") + "/data/" + dsminioFileInfo.getFilePath());
        }
        dsminioFileInfoService.deleteFileInfo(objectIds);
        return AjaxResult.deleteResult("删除成功");

    }


    /**
     * 查询文件列表
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/searchFileList", method = RequestMethod.GET)
    @ApiOperation(value = "查询文件列表")
    public @ResponseBody
    AjaxResult searchFileList(@ApiParam(value = "分页参数", required = true) @RequestParam(value = "startPage", defaultValue = "0") Integer startPage,
                              @ApiParam(value = "folderId", required = true) @RequestParam(value = "folderId") Long folderId) {
        PageHelper.startPage(startPage, 10);
        return AjaxResult.updateResult(new PageInfo<>(dsminioFileInfoService.searchFileInfoByFolderId(folderId)));
    }
}





